import java.util.Arrays;
public class Test {
    /**
     *
     * @param args 2023.2.3 第五课 递归+数组
     */
    public static void main(String[] args) {
        int[] array = {12,4,3,8,19,7};
        System.out.println(myToString(array));//整体输出数组
        bubbleSort(array);
        System.out.println(myToString(array));
    }
    public  static  void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1; j++) {
                if (array[j] == array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[i];
                    array[j+1] = tmp;
                }
            }
            if (!flg) {
                return;
            }
        }
    }

    public static void main18(String[] args) {
        int[] array = {100,12,31,14,15};
        System.out.println(myToString(array));//整体输出数组

        int[] array2 = null;
        System.out.println(Arrays.toString(array2));
    }
    public static String myToString(int[] array){
        if(array == null){
            return "null";
        }
        String ret = "[";
        for(int i = 0;i < array.length; i++){
            ret += array[i];
            if(i != array.length-1) {
                ret += ",";
            }
        }
        ret +="]";
        return ret;
    }

    public static void main17(String[] args) {
        int[] array = {100,12,31,14,15};
        System.out.println(Arrays.toString(array));//整体输出数组
        Arrays.sort(array,0,2);//进行大小排序 区间排序
        System.out.println(Arrays.toString(array));
    }
    public static void main16(String[] args) {
        int[] array = {100,12,31,14,15};
        System.out.println(Arrays.toString(array));//整体输出数组
        Arrays.sort(array);//进行大小排序
        System.out.println(Arrays.toString(array));
    }
    public static void main15(String[] args) {
        int[] array = {1,2,3,4,5};
        System.out.println(Arrays.toString(array));//整体输出数组
    }
    public static void main14(String[] args) {
        int[] ret = func3();
        for(int i = 0; i < ret.length; i++){
            System.out.println(ret[i]);
        }
    }
    public static int[] func3(){
        int[] ret = new int[2];
        ret[0] =  99;
        ret[1] = 199;
        return ret;
    }


    public static void main13(String[] args) {
        int[] array = {1,2,3,4};

        //func1(array);
        func2(array);//对比

        for(int x : array){
            System.out.println(x +" ");
        }
        System.out.println();
    }
    public static void  func1(int[] array){
        array[0] = 99;
    }
    public static void  func2(int[] array){
        array = new int[]{11,22,33,44,55};
    }


    public static void main12(String[] args) {
        int[] array = null;
        System.out.println(array.length);//NullPointerException空指针异常
    }
    //数组遍历
    public static void mai11(String[] args) {
        int[] array = {1,2,3,4};
        for (int i = 0; i < array.length; i++) {//法一 有下标
            System.out.println(array[i]+" ");
        }
        System.out.println();

        //增强for循环，for-each
        for(int x : array){//法二 无下标
            System.out.println(x + " ");
        }
        System.out.println();
    }
    public static void main10(String[] args) {
        int[] array ={1,2,3,4};
        System.out.println(array.length);
    }
    public static void main9(String[] args) {
        int[] array={1,2,3,4};
        System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);
        System.out.println(array[3]);

    }
    public static void main8(String[] args) {
        int[] array ={1,2,3,4};
        int[] array2 = new int[]{1,2,3,4};
        int[] array3 = new int[10];

        int n = 10;
        int[] array4 = new int[n];
    }
    //斐波那契数列
    public static void main7(String[] args) {
        /*System.out.println(fib(5));
        System.out.println(fib(6));
        System.out.println(fib(40));*/

        System.out.println(fib2(5));
        System.out.println(fib2(6));
        System.out.println(fib2(40));
    }
    public static int fib2(int n) {//法二
        if(n == 1 || n == 2){
            return 1;
        }
        int f1 =1;
        int f2 =1;
        int f3 =1;
        for(int i = 3;i <= n;i++){
            f3 = f1 +f2;
            f1 = f2;
            f2 = f3;
        }
        return f3;
    }
    public static int fib(int n){//法一
        if(n == 1){
            return 0;
        }
        if(n == 2){
            return 1;
        }
        return fib(n-1)+fib(n-2);
    }
    //用递归，输入一个非负整数，返回组成它的数字之和，如输入1729，则返回1+7+2+9，和为19
    public static void main6(String[] args) {
        System.out.println(add(123));
    }
    public static int add(int n){
        if(n < 10){
            return n;
        }
        return n % 10 + add(n/10);
    }
    //用递归求1+2+3+……10
    public static void main5(String[] args) {
        System.out.println(sum(10));
    }
    public static  int sum(int n){
        if(n == 1){
            return 1;
        }
        return n + sum(n-1);
    }
    //按顺序打印数字中的每一位eg：1234  1 2 3 4
    public static void main4(String[] args) {
        print(123);
    }
    public static void print(int n){
        if(n < 10){
            System.out.println(n % 10);
            return;
        }
        print(n / 10);
        System.out.println(n % 10);
    }

    public static void main3(String[] args) {
        System.out.println(fac(5));
    }
    public static int fac(int n){
        if(n == 1){
            return 1;
        }
        int tmp = n * fac(n-1);
        return tmp;
    }

    public static void main2(String[] args) {
        func(10);
    }
    public static void func(int a){
        if(a == 1){
            return;
        }
        System.out.println(a);
        func(a-1);
    }//StackOverflowError 栈溢出错误

    public static int max(int a,int b){
        return a > b ? a : b ;
    }
    public static int max(int a,int b,int c){
        return max(max(a,b),c);
    }
}